//
//  IMChatPresenter.m
//  ChatMainDemo
//
//  Created by peiw on 16/1/21.
//  Copyright © 2016年 peiw. All rights reserved.
//

#import "IMChatPresenter.h"
#import "IMChatDisplayData.h"
#import "IMCellDisplayData.h"
#import "NSString+Bounds.h"
#import "IMChatDefines.h"

@implementation IMChatPresenter

- (void)updateView {
    if (canSafePerformSelector(self.interactor, @selector(findUpcomingMessage))) {
        [self.interactor findUpcomingMessage];
    }
}

- (void)foundUpcomingMessages:(NSArray *)messages {
    if (canSafePerformSelector(self.interface, @selector(showUpcomingDisplayData:))) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            IMChatDisplayData *display = [IMChatDisplayData new];
            for (IMCellDisplayData *cellDisplay in messages) {
                switch (cellDisplay.msg.msgType) {
                    case eTFText:{
                        CGSize size = [((NSString *)cellDisplay.msg.content) boundsWithFont:ChatContentFont estimatedSize:CGSizeMake(ChatCellNameWidth, CGFLOAT_MAX)];
                        size.height = MAX(44, size.height);
                        size.width += IMAvatarEdge;
                        cellDisplay.contentSize = size;
                    }
                        break;
                    case eTFImage:
                        
                        break;
                    default:
                        break;
                }
            }
            display.rows = messages;
            dispatch_async(dispatch_get_main_queue(), ^{
               [self.interface showUpcomingDisplayData:display];
           });
            
        });
    }
}


@end
